class Counter3{
    private int count = 0;

    public void add(){
        synchronized (this){
            count++;
        }
    }

    public int getCount(){
        return count;
    }
}
public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        Counter3 counter3 = new Counter3();
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                    counter3.add();
            }
        });

        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                synchronized (counter3){
                    counter3.add();
                    //此时t2中是嵌套锁
                    //第一次加锁是肯定能成功的
                    //第二次加锁时,counter3已经被加锁(按之前理解,针对已经加锁的对象,必须解锁之后,才能加第二次锁)
                }

            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("count= "+counter3.getCount());
    }
}
